//============================================================================================
/**
 * @file	wbtower_codein.c
 * @brief	WIFIバトルタワー数値入力処理
 * @author	Akito Mori
 * @date	08.12.15
 */
//============================================================================================


#include "common.h"
#include <dwc.h>
#include "libdpw/dpw_bt.h"
#include "system/procsys.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/clact_tool.h"
#include "system/msgdata_util.h"
#include "system/wordset.h"
#include "msgdata/msg.naix"
#include "system/wipe.h"
#include "system/fontproc.h"
#include "system/lib_pack.h"
#include "system/lib_pack.h"
#include "system/fontoam.h"
#include "system/window.h"
#include "gflib/touchpanel.h"
#include "system/bmp_menu.h"
#include "system/snd_tool.h"
#include "gflib/strbuf_family.h"
#include "savedata/savedata.h"
#include "savedata/wifilist.h"
#include "savedata/zukanwork.h"
#include "gflib/strbuf.h"
#include "communication/comm_state.h"
#include "communication/comm_def.h"

#include "application/subselect.naix"
#include "sublcd_yesno.h"
#include "msgdata/msg_ev_win.h"

enum{
	SUBLCD_YESNO_START=0,
	SUBLCD_YESNO_WAIT,
	SUBLCD_YESNO_ANIME_WAIT,
	SUBLCD_YESNO_END,
};

#define YESNO_CONTINUE	( 0 )
#define YESNO_END		( 1 )

static void yesno_bg_set( SUBLCD_YESNO_WORK *wk );
static void yesno_bmpwin_draw( SUBLCD_YESNO_WORK *wk );
static void yesno_bmpwin_del( SUBLCD_YESNO_WORK *wk );
static void cursor_act_pos( SUBLCD_YESNO_WORK *wk, int pos );
static void cursor_act_set( SUBLCD_YESNO_WORK *wk );

static int Yesno_Start( SUBLCD_YESNO_WORK *wk );
static int Yesno_Wait( SUBLCD_YESNO_WORK *wk );
static int Yesno_Anime_Wait( SUBLCD_YESNO_WORK *wk );
static int Yesno_End( SUBLCD_YESNO_WORK *wk );


static int (*FuncTable[])( SUBLCD_YESNO_WORK *wk ) = {
	Yesno_Start,
	Yesno_Wait,
	Yesno_Anime_Wait,
	Yesno_End,
};


static const u16 cursor_pos_tbl[][2]={
	{ 128, 72 },
	{ 128, 120 },
};

//============================================================================================
// SE用定義
//============================================================================================
#define WBTOWER_MOVE_SE		(SEQ_SE_DP_SELECT)
#define WBTOWER_DECIDE_SE	(SEQ_SE_DP_SELECT)
#define WBTOWER_FAILED_SE	(SEQ_SE_DP_CUSTOM06)


//==============================================================================
/**
 * @brief   下画面ＹＥＳＮＯ処理初期化
 *
 * @param   wk
 *
 * @retval  none
 */
//==============================================================================
void Sublcd_YesNoInit( SUBLCD_YESNO_WORK *wk )
{
	wk->seq = SUBLCD_YESNO_START;
	wk->pos = wk->start;

	yesno_bg_set( wk );
	yesno_bmpwin_draw( wk );
	cursor_act_set(wk);

	return;
}

//==============================================================================
/**
 * @brief   下画面ＹＥＳＮＯ入力
 *
 * @param   wk
 *
 * @retval  u32
 */
//==============================================================================
u32 Sublcd_YesNoMain( SUBLCD_YESNO_WORK *wk )
{
	if(FuncTable[wk->seq]( wk )==YESNO_END){
		return wk->pos+1;
	}

	return SUBLCD_YESNO_NONE;

}


//------------------------------------------------------------------
/**
 * @brief   選択開始（シーケンス関数）
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Yesno_Start( SUBLCD_YESNO_WORK *wk )
{
	wk->seq = SUBLCD_YESNO_WAIT;
	return YESNO_CONTINUE;
}



static const RECT_HIT_TBL yesno_touch_tbl[]={
 	{ 50,  92, 3, 251, },
	{ 99, 140, 3, 251, },
 	{TP_HIT_END,0,0,0},		// 終了データ

};

//------------------------------------------------------------------
/**
 * @brief   選択待ち（シーケンス関数）
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Yesno_Wait( SUBLCD_YESNO_WORK *wk )
{
	u32 ret=GF_TP_RectHitTrg( yesno_touch_tbl );
	if(ret!=RECT_HIT_NONE){
		Snd_SePlay( WBTOWER_DECIDE_SE );
		if(ret==0){
			wk->pos = 0;
			cursor_act_pos( wk, wk->pos );
			CLACT_AnmChg( wk->cursor, 3 );
			wk->seq = SUBLCD_YESNO_END;
		}else if(ret==1){
			wk->pos = 1;
			cursor_act_pos( wk, wk->pos );
			CLACT_AnmChg( wk->cursor, 3 );
			wk->seq = SUBLCD_YESNO_END;
		}
	}else{
		if(sys.trg&PAD_KEY_UP){
			wk->pos = 0;
			cursor_act_pos( wk, wk->pos );
			Snd_SePlay( WBTOWER_MOVE_SE );
		}else if(sys.trg&PAD_KEY_DOWN){
			wk->pos = 1;
			cursor_act_pos( wk, wk->pos );
			Snd_SePlay( WBTOWER_MOVE_SE );
		}else if(sys.trg&PAD_BUTTON_DECIDE){
			CLACT_AnmChg( wk->cursor, 3 );
			wk->seq = SUBLCD_YESNO_END;
			Snd_SePlay( WBTOWER_DECIDE_SE );
		}else if(sys.trg&PAD_BUTTON_CANCEL){
			wk->pos = 1;
			cursor_act_pos( wk, wk->pos );
			wk->seq = SUBLCD_YESNO_END;
			CLACT_AnmChg( wk->cursor, 3 );
			Snd_SePlay( WBTOWER_DECIDE_SE );
		}
	}

	return YESNO_CONTINUE;
}


//------------------------------------------------------------------
/**
 * @brief   選択終了アニメ待ち（シーケンス関数）
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Yesno_Anime_Wait( SUBLCD_YESNO_WORK *wk )
{
	return YESNO_CONTINUE;
}



//------------------------------------------------------------------
/**
 * @brief   選択終了（シーケンス関数）
 *
 * @param   wk
 *
 * @retval  int
 */
//------------------------------------------------------------------
static int Yesno_End( SUBLCD_YESNO_WORK *wk )
{
	if(CLACT_AnmActiveCheck( wk->cursor )){
		return YESNO_CONTINUE;
	}
	GF_BGL_VisibleSet( wk->frame, VISIBLE_OFF );
	GF_BGL_VisibleSet( wk->text_frame, VISIBLE_OFF );
	yesno_bmpwin_del( wk );
	CLACT_SetDrawFlag( wk->cursor, 0 );

	return YESNO_END;
}



// スクリーン転送サイズ
#define SUBLCD_YESNO_SCREEN_SIZE	( 32*24 )

//------------------------------------------------------------------
/**
 * @brief   ＢＧグラフィック転送
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void yesno_bg_set( SUBLCD_YESNO_WORK *wk )
{
	NNSG2dScreenData *scrDat;
	ARCHANDLE* p_handle = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, wk->heapid );
	void *buf;
	u16 *adr,*data;
	int i;

	// パレット

	if(wk->frame<=GF_BGL_FRAME3_M){
	    ArcUtil_HDL_PalSet(    p_handle, NARC_subselect_bmpmenu_nclr, PALTYPE_MAIN_BG, wk->pal*32, 32,  wk->heapid);
		SystemFontPaletteLoad( PALTYPE_MAIN_BG, wk->text_pal*32, wk->heapid );
	}else{
	    ArcUtil_HDL_PalSet(    p_handle, NARC_subselect_bmpmenu_nclr, PALTYPE_SUB_BG,  wk->pal*32, 32,  wk->heapid);
		SystemFontPaletteLoad( PALTYPE_SUB_BG, wk->text_pal*32, wk->heapid );
	}

	// サブ画面BG1キャラ転送
	ArcUtil_HDL_BgCharSet( p_handle, NARC_subselect_bmpmenu_bg_ncgr, wk->bgl, wk->frame, 0, 0, 0, wk->heapid);

	// サブ画面BG1スクリーン転送
	buf = ArcUtil_HDL_ScrnDataGet( p_handle, NARC_subselect_bmpmenu_yn_nscr, 0, &scrDat, wk->heapid);


	adr = (u16*)GF_BGL_ScreenAdrsGet( wk->bgl, wk->frame );
	data = (u16*)scrDat->rawData;
	for(i=0;i<SUBLCD_YESNO_SCREEN_SIZE;i++){
		adr[i] = (data[i] & 0x0fff) | (wk->pal<<12);
	}
	OS_Printf("adr = %08x\n", adr);

	GF_BGL_LoadScreenReq( wk->bgl, wk->frame );
	sys_FreeMemoryEz( buf );

	ArchiveDataHandleClose( p_handle );

	// ＢＧ面表示ＯＮ
	GF_BGL_VisibleSet( wk->frame, VISIBLE_ON );
	GF_BGL_VisibleSet( wk->text_frame, VISIBLE_ON );



}



#define YESNO_BMP_X		( 12 )
#define YESNO_BMP_Y1	(  8 )
#define YESNO_BMP_Y2	( 14 )
#define YESNO_BMP_W		(  8 )
#define YESNO_BMP_H		(  2 )

//------------------------------------------------------------------
/**
 * @brief   「はい」「いいえ」描画
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void yesno_bmpwin_draw( SUBLCD_YESNO_WORK *wk )
{
	MSGDATA_MANAGER *msgman = MSGMAN_Create( MSGMAN_TYPE_DIRECT, ARC_MSG, NARC_msg_ev_win_dat, wk->heapid );
	STRBUF *yesstr, *nostr;

	GF_BGL_BmpWinAdd( wk->bgl, &wk->yesnostr[0], wk->text_frame,
					  YESNO_BMP_X, YESNO_BMP_Y1, YESNO_BMP_W, YESNO_BMP_H,
					  wk->text_pal, wk->text_ofs	 );
	GF_BGL_BmpWinDataFill(  &wk->yesnostr[0], 0 );

	GF_BGL_BmpWinAdd( wk->bgl, &wk->yesnostr[1], wk->text_frame,
					  YESNO_BMP_X, YESNO_BMP_Y2, YESNO_BMP_W, YESNO_BMP_H,
					  wk->text_pal, wk->text_ofs+YESNO_BMP_W*YESNO_BMP_H );
	GF_BGL_BmpWinDataFill(  &wk->yesnostr[1], 0 );

	yesstr = MSGMAN_AllocString( msgman, msg_ev_win_046_01 );
	nostr  = MSGMAN_AllocString( msgman, msg_ev_win_046_02 );

	GF_STR_PrintColor(
		&wk->yesnostr[0], FONT_TOUCH, yesstr, 0, 0, MSG_ALLPUT,
		GF_PRINTCOLOR_MAKE(15, 1, 0), NULL );
	GF_STR_PrintColor(
		&wk->yesnostr[1], FONT_TOUCH, nostr, 0, 0, MSG_ALLPUT,
		GF_PRINTCOLOR_MAKE(15, 1, 0), NULL );

	GF_BGL_BmpWinOn( &wk->yesnostr[0] );
	GF_BGL_BmpWinOn( &wk->yesnostr[1] );

	STRBUF_Delete(nostr);
	STRBUF_Delete(yesstr);

	MSGMAN_Delete( msgman );
}

//------------------------------------------------------------------
/**
 * @brief   BMPWIN解放
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void yesno_bmpwin_del( SUBLCD_YESNO_WORK *wk )
{

	GF_BGL_BmpWinOff( &wk->yesnostr[1] );
	GF_BGL_BmpWinOff( &wk->yesnostr[0] );

	GF_BGL_BmpWinDel( &wk->yesnostr[1] );
	GF_BGL_BmpWinDel( &wk->yesnostr[0] );

}


//------------------------------------------------------------------
/**
 * @brief   セルアクター表示開始処理
 *
 * @param   wk
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void cursor_act_set( SUBLCD_YESNO_WORK *wk )
{
	CLACT_SetDrawFlag( wk->cursor, 1 );
	cursor_act_pos( wk, wk->start );
}

//------------------------------------------------------------------
/**
 * @brief   カーソル座標設定
 *
 * @param   wk
 * @param   pos
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void cursor_act_pos( SUBLCD_YESNO_WORK *wk, int pos )
{
	VecFx32 mat;
	mat.x = cursor_pos_tbl[pos][0]*FX32_ONE;
	mat.y = (cursor_pos_tbl[pos][1]+wk->subdistance)*FX32_ONE;
	mat.z = 0;

	CLACT_SetMatrix( wk->cursor, &mat );

}